package com.ruoyi.web.controller.student;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.domain.BasisPosition;
import com.ruoyi.service.IBasisPositionService;
import com.ruoyi.service.IBasisTaskPlanService;
import com.ruoyi.service.TcpClientService;
import com.ruoyi.service.impl.TcpClientServiceImpl;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@RequestMapping("/api/tcp")
public class TcpClientController extends BaseController {
    @Autowired
    private TcpClientService tcpClientService;

    @Autowired
    private IBasisPositionService iBasisPositionService;

    private static  List<TcpClientServiceImpl.CSigSpec>  cachedValue;

    private static  List<double[]>  Alldata;

    @Autowired
    private IBasisTaskPlanService iBasisTaskPlanService;


    private static BasisPosition  basisPositionS;

    private static String retrunTimeS;

    @GetMapping("/send")
    public  List<double[]> send(double min, double max,double endMax,String caseName) throws IOException {
        Long userid = getUserId();
        List<TcpClientServiceImpl.CSigSpec> cSigSpecList = tcpClientService.sendMessage(min,max,caseName,userid);
        cachedValue = cSigSpecList;
        //生成底噪数据
        List<double[]> generateNoiseData = generateNoiseData(min,max);

        double lengthAll = calculateDataPoints((int)min+"M", (int)(min+40)+"M");
        List<double[]> zs = new ArrayList<>();
        List<double[]> returnData = new ArrayList<>();
        for(TcpClientServiceImpl.CSigSpec cSigSpec : cSigSpecList){
            double firstS = cSigSpec.dbStartFreq;
            double firstD = firstS;
            float[] dataS = cSigSpec.fFFTData;

            for (int i = 0 ; i< dataS.length ; i++){
                double[] doubles = new double[]{firstD, dataS[i]};
                firstD = firstD + 40/lengthAll; // (最大值-最小值)/ lengthAll
                firstD = round(firstD,3);
                zs.add(doubles);
            }

        }
        // 替换逻辑
        for(double[] item1 : zs) {
            for(int i = 0; i < generateNoiseData.size(); i++) {
                if(generateNoiseData.get(i)[0] == item1[0]) {
                    generateNoiseData.set(i, item1);
                    break;
                }
            }
        }
        Alldata = generateNoiseData;
        int ggi = 0;
        for(int gg = 0 ; gg < generateNoiseData.size() ; gg++){
            if(gg%10 == 0 && gg != generateNoiseData.size()-1){
                double[] doubles = generateNoiseData.get(gg);
                returnData.add(doubles);
            }
            if(gg == generateNoiseData.size()-1){
                double[] doubles = generateNoiseData.get(gg);
                returnData.add(doubles);
            }

        }
        return returnData;
    }
   // private final MyDll dll = MyDll.INSTANCE;
    @GetMapping("/sendHand")
    public  List<double[]> sendHand(double min, double max,double startPort, double endPort, String time , double longitude , double latitude,String loadingId,String caseName) throws IOException, ParseException {

            Long userid = getUserId();
        //生成底噪数据
        List<double[]> generateNoiseData = generateNoiseData(startPort,endPort);
        Instant instant = Instant.ofEpochMilli(Long.parseLong(time));

        // 指定时区，例如使用UTC时区
        ZoneId zoneId = ZoneId.systemDefault(); // 或者使用ZoneId.of("America/New_York")等具体时区

        // 将Instant转换为LocalDateTime
        LocalDateTime dateTime = instant.atZone(zoneId).toLocalDateTime();

        // 定义日期时间的格式化模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化输出
        String formattedDateTime = dateTime.format(formatter);
        //String formattedDateTime = "2025-07-10 08:10:10";
        System.out.println(formattedDateTime);

        //查找本人的任务时间
        String retrunTime = iBasisTaskPlanService.queryTime(loadingId,String.valueOf(userid));
        retrunTimeS = retrunTime;

        // 获取分隔符位置
        int separatorIndex = retrunTime.indexOf("--");

        // 截取第一个时间
        String firstTime = retrunTime.substring(0, separatorIndex);

        // 截取第二个时间
        String secondTime = retrunTime.substring(separatorIndex + 2);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf.parse(firstTime);
        Date date2 = sdf.parse(secondTime);
        Date date3 = sdf.parse(formattedDateTime);
        int result = date1.compareTo(date3);
        int result1 = date3.compareTo(date2);
        if(result < 0 && result1 < 0){
        //if(true){
            //查找阵地位置

            BasisPosition basisPosition = iBasisPositionService.selectBasisPositionByUserID(loadingId,userid);
            basisPositionS = basisPosition;
            List<TcpClientServiceImpl.CSigSpec> cSigSpecList = tcpClientService.sendMessage(min,max,caseName,userid);
            cachedValue = cSigSpecList;


            double lengthAll = calculateDataPoints((int)min+"M", (int)max +"M");
            List<double[]> zs = new ArrayList<>();
            List<double[]> returnData = new ArrayList<>();
            for(TcpClientServiceImpl.CSigSpec cSigSpec : cSigSpecList){
                double firstS = cSigSpec.dbStartFreq;
                double firstD = firstS;
                float[] dataS = cSigSpec.fFFTData;
                Pointer inputPtr = new Memory(dataS.length * 4); // float占4字节
                inputPtr.write(0, dataS, 0, dataS.length);
                double longitude2 = Double.parseDouble(basisPosition.getLongitude());
                double latitude2 = Double.parseDouble(basisPosition.getLatitude());
//                Pointer resultPtr = dll.FSPL(inputPtr,  dataS.length,
//                        longitude, latitude, 5000,
//                        longitude2,  latitude2, 0,
//                        (firstD + firstS)/2, 1);
//                float[] callFSPL = resultPtr.getFloatArray(0, dataS.length);

                for (int i = 0 ; i< dataS.length ; i++){
                    double[] doubles = new double[]{firstD, dataS[i]};
                    firstD = firstD + (max - min)/lengthAll; // (最大值-最小值)/ lengthAll
                    firstD = round(firstD,3);
                    zs.add(doubles);
                }

            }
            // 替换逻辑
            for(double[] item1 : zs) {
                for(int i = 0; i < generateNoiseData.size(); i++) {
                    if(generateNoiseData.get(i)[0] == item1[0] && generateNoiseData.get(i)[1] < item1[1]) {
                        generateNoiseData.set(i, item1);
                        break;
                    }
                }
            }
            Alldata = generateNoiseData;
            int ggi = 0;
            //double re = isFloorDifferenceGreaterThan20(endPort,startPort) ;
            if(isFloorDifferenceGreaterThan20(endPort,startPort) ) {
                for (int gg = 0; gg < generateNoiseData.size(); gg++) {

                    if (gg % 40 == 0 && gg != generateNoiseData.size() - 1) {
                        double[] doubles = generateNoiseData.get(gg);
                        returnData.add(doubles);
                    }
                    if (gg == generateNoiseData.size() - 1) {
                        double[] doubles = generateNoiseData.get(gg);
                        returnData.add(doubles);
                    }

                }
                return returnData;
            }else{
                return generateNoiseData;
            }

        }else{
            return generateNoiseData;
        }


    }

    @GetMapping("/sendHand2")
    public  List<double[]> sendHand2(double min, double max,double startPort, double endPort,String caseName) throws IOException {
        //生成底噪数据
        Long userid = getUserId();
        if(cachedValue == null){
            List<TcpClientServiceImpl.CSigSpec> cSigSpecList = tcpClientService.sendMessage(min,max,caseName,userid);
            cachedValue = cSigSpecList;
        }

        List<double[]> generateNoiseData = generateNoiseData(startPort,endPort);

        double lengthAll = calculateDataPoints((int)min+"M", (int)max +"M");
        List<double[]> zs = new ArrayList<>();
        List<double[]> returnData = new ArrayList<>();
        List<TcpClientServiceImpl.CSigSpec>  newcachedValue = new ArrayList<>();

        for(TcpClientServiceImpl.CSigSpec cSigSpec : cachedValue){
            double firstS = cSigSpec.dbStartFreq;
            double firstD = firstS;
            float[] dataS = cSigSpec.fFFTData;
            float[] newData = adjustSecondValue(dataS);
            TcpClientServiceImpl.CSigSpec newcSigSpec = new TcpClientServiceImpl.CSigSpec();
            newcSigSpec.dbStartFreq = cSigSpec.dbStartFreq;
            newcSigSpec.dbEndFreq = cSigSpec.dbEndFreq;
            newcSigSpec.nFFTDataLen = cSigSpec.nFFTDataLen;
            newcSigSpec.fFFTData = newData;
            newcachedValue.add(newcSigSpec);
        }
        for(TcpClientServiceImpl.CSigSpec cSigSpec : newcachedValue){
            double firstS = cSigSpec.dbStartFreq;
            double firstD = firstS;
            float[] dataS = cSigSpec.fFFTData;

            for (int i = 0 ; i< dataS.length ; i++){
                double[] doubles = new double[]{firstD, (dataS[i]-2)};
                firstD = firstD + (max - min)/lengthAll; // (最大值-最小值)/ lengthAll
                firstD = round(firstD,3);
                zs.add(doubles);
            }
        }
        // 替换逻辑
        for(double[] item1 : zs) {
            for(int i = 0; i < generateNoiseData.size(); i++) {
                if(generateNoiseData.get(i)[0] == item1[0]) {
                    generateNoiseData.set(i, item1);
                    break;
                }
            }
        }
        Alldata = generateNoiseData;
        int ggi = 0;
        for(int gg = 0 ; gg < generateNoiseData.size() ; gg++){
            if( gg%10 == 0 && gg != generateNoiseData.size()-1){
                double[] doubles = generateNoiseData.get(gg);
                returnData.add(doubles);
            }
            if(gg == generateNoiseData.size()-1){
                double[] doubles = generateNoiseData.get(gg);
                returnData.add(doubles);
            }

        }
        return returnData;
    }

    @GetMapping("/getvalue")
    public List<double[]> getValue(double min, double max,double endMax,String caseName) throws IOException {

        //修改原频谱数据


        // 创建随机数生成器
        Random random = new Random();

        // 波动幅度为1
        double fluctuationRange = 1.0;
        List<TcpClientServiceImpl.CSigSpec>  newcachedValue = new ArrayList<>();
        List<double[]> returnData = new ArrayList<>();
        if(cachedValue != null){
            // 对每个频谱数据进行随机波动
            for(TcpClientServiceImpl.CSigSpec cSigSpec : cachedValue){
                double firstS = cSigSpec.dbStartFreq;
                double firstD = firstS;
                float[] dataS = cSigSpec.fFFTData;
                float[] newData = adjustSecondValue(dataS);
                TcpClientServiceImpl.CSigSpec newcSigSpec = new TcpClientServiceImpl.CSigSpec();
                newcSigSpec.dbStartFreq = cSigSpec.dbStartFreq;
                newcSigSpec.dbEndFreq = cSigSpec.dbEndFreq;
                newcSigSpec.nFFTDataLen = cSigSpec.nFFTDataLen;
                newcSigSpec.fFFTData = newData;
                newcachedValue.add(newcSigSpec);
            }
            //生成底噪数据
            List<double[]> generateNoiseData = generateNoiseData(min,max);

            double lengthAll = calculateDataPoints((int)min+"M", (int)max+"M");
            List<double[]> zs = new ArrayList<>();
            for(TcpClientServiceImpl.CSigSpec cSigSpec : newcachedValue){
                double firstS = cSigSpec.dbStartFreq;
                double firstD = firstS;
                float[] dataS = cSigSpec.fFFTData;

                for (int i = 0 ; i< dataS.length ; i++){
                    double[] doubles = new double[]{firstD, dataS[i]};
                    firstD = firstD + ((int)max-(int)min)/lengthAll;
                    firstD = round(firstD,3);
                    zs.add(doubles);
                }

            }
            // 替换逻辑
            for(double[] item1 : zs) {
                for(int i = 0; i < generateNoiseData.size(); i++) {
                    if(generateNoiseData.get(i)[0] == item1[0]) {
                        generateNoiseData.set(i, item1);
                        break;
                    }
                }
            }
            int ggi = 0;
            for(int gg = 0 ; gg < generateNoiseData.size() ; gg++){
                if(gg%40 == 0 && gg != generateNoiseData.size()-1){
                    double[] doubles = generateNoiseData.get(gg);
                    returnData.add(doubles);
                }
                if(gg == generateNoiseData.size()-1){
                    double[] doubles = generateNoiseData.get(gg);
                    returnData.add(doubles);
                }

            }
            return returnData;
        }else{
            return send(min, max, endMax,caseName);
        }
    }

    @GetMapping("/getvalue2")
    public List<double[]> getValue2(double min, double max,double startPort, double endPort,String time , double longitude , double latitude,String loadingId,String caseName) throws IOException, ParseException {
        Long userid = getUserId();

        List<double[]> generateNoiseData = generateNoiseData(startPort,endPort);

        Instant instant = Instant.ofEpochMilli(Long.parseLong(time));

        // 指定时区，例如使用UTC时区
        ZoneId zoneId = ZoneId.systemDefault(); // 或者使用ZoneId.of("America/New_York")等具体时区

        // 将Instant转换为LocalDateTime
        LocalDateTime dateTime = instant.atZone(zoneId).toLocalDateTime();

        // 定义日期时间的格式化模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化输出
        String formattedDateTime = dateTime.format(formatter);

        //String formattedDateTime = "2025-07-10 08:10:10";
        System.out.println(formattedDateTime);
        System.out.println(formattedDateTime);
        String retrunTime = "";
        //查找本人的任务时间
        if(retrunTimeS != null && !"".equals(retrunTimeS)){
            retrunTime = retrunTimeS;
        }else{
            retrunTime = iBasisTaskPlanService.queryTime(loadingId,String.valueOf(userid));
        }
        // 获取分隔符位置
        int separatorIndex = retrunTime.indexOf("--");

        // 截取第一个时间
        String firstTime = retrunTime.substring(0, separatorIndex);

        // 截取第二个时间
        String secondTime = retrunTime.substring(separatorIndex + 2);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf.parse(firstTime);
        Date date2 = sdf.parse(secondTime);
        Date date3 = sdf.parse(formattedDateTime);
        int result = date1.compareTo(date3);
        int result1 = date3.compareTo(date2);
        if(true) {
         //  if(result < 0 && result1 < 0) {
            List<TcpClientServiceImpl.CSigSpec> newcachedValue = new ArrayList<>();
            double lengthAll = calculateDataPoints((int) min + "M", (int) max + "M");
            List<double[]> zs = new ArrayList<>();
            List<double[]> returnData = new ArrayList<>();
            BasisPosition basisPosition = new BasisPosition();
            if (basisPositionS != null) {
                basisPosition = basisPositionS;
            } else {
                basisPosition = iBasisPositionService.selectBasisPositionByUserID(loadingId, userid);
                basisPositionS = basisPosition;
            }

            // 对每个频谱数据进行随机波动
            if (cachedValue != null) {
                for (TcpClientServiceImpl.CSigSpec cSigSpec : cachedValue) {
                    double firstS = cSigSpec.dbStartFreq;
                    double firstD = firstS;
                    float[] dataS = cSigSpec.fFFTData;
                    float[] newData = adjustSecondValue(dataS);
                    TcpClientServiceImpl.CSigSpec newcSigSpec = new TcpClientServiceImpl.CSigSpec();
                    newcSigSpec.dbStartFreq = cSigSpec.dbStartFreq;
                    newcSigSpec.dbEndFreq = cSigSpec.dbEndFreq;
                    newcSigSpec.nFFTDataLen = cSigSpec.nFFTDataLen;
                    newcSigSpec.fFFTData = newData;
                    newcachedValue.add(newcSigSpec);
                }
                for (TcpClientServiceImpl.CSigSpec cSigSpec : newcachedValue) {
                    double firstS = cSigSpec.dbStartFreq;
                    double firstD = firstS;
                    float[] dataS = cSigSpec.fFFTData;

                    Pointer inputPtr = new Memory(dataS.length * 4); // float占4字节
                    inputPtr.write(0, dataS, 0, dataS.length);

//                    Pointer resultPtr = dll.FSPL(inputPtr, dataS.length,
//                            longitude, latitude, 5000,
//                            Double.parseDouble(basisPosition.getLongitude()), Double.parseDouble(basisPosition.getLatitude()), 0,
//                            (firstD + firstS) / 2, 1);
//                    float[] callFSPL = resultPtr.getFloatArray(0, dataS.length);


                    for (int i = 0; i < dataS.length; i++) {
                        double[] doubles = new double[]{firstD, (dataS[i] - 2)};
                        firstD = firstD + (max - min) / lengthAll; // (最大值-最小值)/ lengthAll
                        firstD = round(firstD, 3);
                        zs.add(doubles);
                    }

                }


                // 替换逻辑
                for (double[] item1 : zs) {
                    for (int i = 0; i < generateNoiseData.size(); i++) {
                        if (generateNoiseData.get(i)[0] == item1[0] && generateNoiseData.get(i)[1] < item1[1]) {
                            generateNoiseData.set(i, item1);
                            break;
                        }
                    }
                }
                int ggi = 0;
                double re = endPort - startPort ;
                if(isFloorDifferenceGreaterThan20(endPort,startPort) ){
                    for (int gg = 0; gg < generateNoiseData.size(); gg++) {
                        if (gg % 40 == 0 && gg != generateNoiseData.size() - 1) {
                            double[] doubles = generateNoiseData.get(gg);
                            returnData.add(doubles);
                        }
                        if (gg == generateNoiseData.size() - 1) {
                            double[] doubles = generateNoiseData.get(gg);
                            returnData.add(doubles);
                        }

                    }
                    return returnData;
                }else {
                    return generateNoiseData;
                }
            } else {
                return generateNoiseData;
            }
        }else{
            return  getValue2( min,  max, startPort,  endPort, time ,  longitude ,  latitude, loadingId, caseName);
        }
    }


    @GetMapping("/getvalue4")
    public List<double[]> getValue4(double min, double max,double startPort, double endPort,String time , double longitude , double latitude,String loadingId,String caseName) throws IOException, ParseException {
        Long userid = getUserId();

        List<double[]> generateNoiseData = generateNoiseData(startPort,endPort);

        Instant instant = Instant.ofEpochMilli(Long.parseLong(time));

        // 指定时区，例如使用UTC时区
        ZoneId zoneId = ZoneId.systemDefault(); // 或者使用ZoneId.of("America/New_York")等具体时区

        // 将Instant转换为LocalDateTime
        LocalDateTime dateTime = instant.atZone(zoneId).toLocalDateTime();

        // 定义日期时间的格式化模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化输出
        String formattedDateTime = dateTime.format(formatter);

        //String formattedDateTime = "2025-07-10 08:10:10";
        System.out.println(formattedDateTime);
        System.out.println(formattedDateTime);
        String retrunTime = "";
        //查找本人的任务时间
        if(retrunTimeS != null && !"".equals(retrunTimeS)){
            retrunTime = retrunTimeS;
        }else{
            retrunTime = iBasisTaskPlanService.queryTime(loadingId,String.valueOf(userid));
        }
        // 获取分隔符位置
        int separatorIndex = retrunTime.indexOf("--");

        // 截取第一个时间
        String firstTime = retrunTime.substring(0, separatorIndex);

        // 截取第二个时间
        String secondTime = retrunTime.substring(separatorIndex + 2);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = sdf.parse(firstTime);
        Date date2 = sdf.parse(secondTime);
        Date date3 = sdf.parse(formattedDateTime);
        int result = date1.compareTo(date3);
        int result1 = date3.compareTo(date2);
        if(result < 0 && result1 < 0) {
       // if(true) {
            List<TcpClientServiceImpl.CSigSpec> newcachedValue = new ArrayList<>();
            double lengthAll = calculateDataPoints((int) min + "M", (int) max + "M");
            List<double[]> zs = new ArrayList<>();
            List<double[]> returnData = new ArrayList<>();
            BasisPosition basisPosition = new BasisPosition();
            if (basisPositionS != null) {
                basisPosition = basisPositionS;
            } else {
                basisPosition = iBasisPositionService.selectBasisPositionByUserID(loadingId, userid);
                basisPositionS = basisPosition;
            }

            // 对每个频谱数据进行随机波动
            if (cachedValue != null) {
                for (TcpClientServiceImpl.CSigSpec cSigSpec : cachedValue) {
                    double firstS = cSigSpec.dbStartFreq;
                    double firstD = firstS;
                    float[] dataS = cSigSpec.fFFTData;
                    float[] newData = adjustSecondValue(dataS);
                    TcpClientServiceImpl.CSigSpec newcSigSpec = new TcpClientServiceImpl.CSigSpec();
                    newcSigSpec.dbStartFreq = cSigSpec.dbStartFreq;
                    newcSigSpec.dbEndFreq = cSigSpec.dbEndFreq;
                    newcSigSpec.nFFTDataLen = cSigSpec.nFFTDataLen;
                    newcSigSpec.fFFTData = newData;
                    newcachedValue.add(newcSigSpec);
                }
                for (TcpClientServiceImpl.CSigSpec cSigSpec : newcachedValue) {
                    double firstS = cSigSpec.dbStartFreq;
                    double firstD = firstS;
                    float[] dataS = cSigSpec.fFFTData;

                    Pointer inputPtr = new Memory(dataS.length * 4); // float占4字节
                    inputPtr.write(0, dataS, 0, dataS.length);

//                    Pointer resultPtr = dll.FSPL(inputPtr, dataS.length,
//                            longitude, latitude, 5000,
//                            Double.parseDouble(basisPosition.getLongitude()), Double.parseDouble(basisPosition.getLatitude()), 0,
//                            (firstD + firstS) / 2, 1);
//                    float[] callFSPL = resultPtr.getFloatArray(0, dataS.length);


                    for (int i = 0; i < dataS.length; i++) {
                        double[] doubles = new double[]{firstD, (dataS[i] - 2)};
                        firstD = firstD + (max - min) / lengthAll; // (最大值-最小值)/ lengthAll
                        firstD = round(firstD, 3);
                        zs.add(doubles);
                    }

                }


                // 替换逻辑
                for (double[] item1 : zs) {
                    for (int i = 0; i < generateNoiseData.size(); i++) {
                        if (generateNoiseData.get(i)[0] == item1[0] && generateNoiseData.get(i)[1] < item1[1]) {
                            generateNoiseData.set(i, item1);
                            break;
                        }
                    }
                }
                int ggi = 0;
                for (int gg = 0; gg < generateNoiseData.size(); gg++) {

                        double[] doubles = generateNoiseData.get(gg);


                }
                return generateNoiseData;
            } else {
                return generateNoiseData;
            }
        }else{
            return  getValue4( min,  max, startPort,  endPort, time ,  longitude ,  latitude, loadingId, caseName);
        }
    }





    @GetMapping("/getvalue3")
    public List<double[]> getValue3(double min, double max) throws IOException {
        List<double[]> returnData = new ArrayList<>();
        for(int i = 0; i < Alldata.size() ; i++){
            if(Alldata.get(i)[0] >= min && Alldata.get(i)[0] <= max) {
                double[] doubles = Alldata.get(i);
                returnData.add(doubles);
            }
        }
        return returnData;
    }


    public static float[] adjustSecondValue(float[] input) {
        Random rand = new Random();
        float[] result = new float[input.length];

        for (int i = 0; i < input.length; i++) {
            float offset = (rand.nextFloat() * 2) - 3; // [-1,1]
            result[i] = input[i] + offset;
        }
        return result;
    }

    public  float[] appendArrays(float[] first, float[] second) {
        float[] result = new float[first.length + second.length];
        return result;
    }

    private List<Double> generateBandData(int count) {
        List<Double> band = new ArrayList<>(count);
        Random rand = new Random();
        for(int i=0; i<count; i++){
            band.add(70 + rand.nextDouble() * 30);
        }
        return band;
    }
    private Map<String, Object> createBand(String name, float[] data) {
        Map<String, Object> band = new HashMap<>();
        band.put("name", name);
        band.put("data", data);
        return band;
    }

    /**
     * 生成底噪数据
     * @return
     */

    public static List<double[]> generateNoiseData(double min, double max) {
        List<double[]> result = new ArrayList<>();
        Random random = new Random();
        double length = calculateDataPoints((int)min+"M", (int)max+"M");
        double baseValue = -120; // 基础值
        double amplitude = 5; // 幅度

        // 创建二维数组
        double dd = min;
        for (int i = 0; i < length; i++) {
            double[] doubles = new double[]{ round(dd,3), baseValue + (random.nextDouble() * 2 * amplitude - amplitude)};
            dd = dd + ((int)max-(int)min)/length;
            result.add(doubles);
        }
        return result;
    }

    // 将带单位的频率字符串转换为数值(Hz)
    public static long parseFrequency(String freqStr) {
        freqStr = freqStr.toUpperCase();
        if (freqStr.endsWith("K")) {
            return Long.parseLong(freqStr.substring(0, freqStr.length()-1)) * 1000L;
        } else if (freqStr.endsWith("M")) {
            return Long.parseLong(freqStr.substring(0, freqStr.length()-1)) * 1000_000L;
        } else if (freqStr.endsWith("G")) {
            return Long.parseLong(freqStr.substring(0, freqStr.length()-1)) * 1000_000_000L;
        }
        return Long.parseLong(freqStr);
    }

    // 计算两个频率间的数据点数量并除以1000
    public static double calculateDataPoints(String startFreq, String endFreq) {
        long start = parseFrequency(startFreq);
        long end = parseFrequency(endFreq);
        if (start >= end) {
            throw new IllegalArgumentException("起始频率必须小于结束频率");
        }
        return (end - start) / 1000.0;
    }

    public static void main(String[] args) {
        generateNoiseData(120,120.5);
    }

    public static double round(double value, int places) {
        if (places < 0) {
            throw new IllegalArgumentException("小数位数不能为负数");
        }
        BigDecimal bd = BigDecimal.valueOf(value);
        bd = bd.setScale(places, RoundingMode.HALF_UP);
        return bd.doubleValue();
    }
    public static boolean isFloorDifferenceGreaterThan20(double a, double b) {
        // 计算差值并向下取整
        long flooredDiff = (long) Math.floor(Math.abs(a - b));
        return flooredDiff > 20;
    }


}
